package managedbean.SCM.materialRequest;

import entity.MRP.MaterialRequirement.WeeklyMaterialReq;
import entity.SCM.MaterialLine;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.stateless.MRP.MaterialRequirement.WeeklyMaterialReqBeanLocal;
import session.stateless.SCM.MaterialRequestBeanLocal;

@ManagedBean(name = "viewWeeklyRequestFromMRPManagerBean")
@ViewScoped
public class ViewWeeklyRequestFromMRPManagerBean {

    /** Creates a new instance of ViewWeeklyRequestFromMRP */
    public ViewWeeklyRequestFromMRPManagerBean() {
    }
    
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    @EJB
    private WeeklyMaterialReqBeanLocal weeklyMaterialReqBeanLocal;
    @EJB
    private MaterialRequestBeanLocal materialRequestBeanLocal;
    private WeeklyMaterialReq selectedWeeklyMaterialReq;
    private String statusMessage;

    public WeeklyMaterialReq getSelectedWeeklyMaterialReq() {
        return selectedWeeklyMaterialReq;
    }

    public void setSelectedWeeklyMaterialReq(WeeklyMaterialReq selectedWeeklyMaterialReq) {
        this.selectedWeeklyMaterialReq = selectedWeeklyMaterialReq;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public List<WeeklyMaterialReq> getWeeklyMaterialReq() {
        return weeklyMaterialReqBeanLocal.getPendingWeeklyMaterialReq();
    }

    public String getWeekOfYear(WeeklyMaterialReq wMR) throws ParseException {
        Calendar c = new GregorianCalendar();
        int year = Integer.parseInt(wMR.getPlannedWeeklyDemand().getPlannedMonthlyDemand().getMonthName().split("/")[1]);
        int startDate = Integer.parseInt(wMR.getPlannedWeeklyDemand().getPlannedDailyDemand().get(0).getDayName().split("/")[0]);
        int endDate = Integer.parseInt(wMR.getPlannedWeeklyDemand().getPlannedDailyDemand().get(wMR.getPlannedWeeklyDemand().getPlannedDailyDemand().size() - 1).getDayName().split("/")[0]);

        int month = Integer.parseInt(wMR.getPlannedWeeklyDemand().getPlannedDailyDemand().get(0).getDayName().split("/")[1]);


        String startWeek = startDate + "/" + month + "/" + year;
        String endWeek = endDate + "/" + month + "/" + year;

//        Date temp_date = new Date();
//        temp_date=dateFormat.parse(startWeek);
//        Calendar cal = Calendar.getInstance();
//        cal.setTime(temp_date);
//        cal.add(Calendar.DATE, 4);

        //    String endWeek = dateFormat.format(cal.getTime()).toString();
        System.out.println(startWeek + "  " + endWeek);

        return startWeek + " - " + endWeek;
    }

    public String getProcessingWeekOfYear(WeeklyMaterialReq wMR) throws ParseException {
        Calendar c = new GregorianCalendar();
        int year = Integer.parseInt(wMR.getPlannedWeeklyDemand().getPlannedMonthlyDemand().getMonthName().split("/")[1]);
        int startDate = Integer.parseInt(wMR.getPlannedWeeklyDemand().getPlannedDailyDemand().get(0).getDayName().split("/")[0]);
        int endDate = Integer.parseInt(wMR.getPlannedWeeklyDemand().getPlannedDailyDemand().get(wMR.getPlannedWeeklyDemand().getPlannedDailyDemand().size() - 1).getDayName().split("/")[0]);

        int month = Integer.parseInt(wMR.getPlannedWeeklyDemand().getPlannedDailyDemand().get(0).getDayName().split("/")[1]);


        String startWeek = startDate + "/" + month + "/" + year;
        String endWeek = endDate + "/" + month + "/" + year;

        Date temp_startDate = new Date();
        temp_startDate = dateFormat.parse(startWeek);
        Calendar cal = Calendar.getInstance();
        cal.setTime(temp_startDate);
        cal.add(Calendar.DATE, -21);
        startWeek = dateFormat.format(cal.getTime()).toString();

        Date temp_endDate = new Date();
        temp_endDate = dateFormat.parse(startWeek);
        Calendar cal_temp = Calendar.getInstance();
        cal_temp.setTime(temp_endDate);
        cal_temp.add(Calendar.DATE, -21);
        endWeek = dateFormat.format(cal_temp.getTime()).toString();

        return startWeek + " - " + endWeek;
    }

    public void convertToMaterialRequest(ActionEvent event) throws Exception {

        List<MaterialLine> materialLineList = new ArrayList<MaterialLine>();
        MaterialLine materialLine = new MaterialLine();

        List<MaterialLine> tempMaterialLines = weeklyMaterialReqBeanLocal.createMaterialLine(selectedWeeklyMaterialReq.getId());
        for (int i = 0; i < tempMaterialLines.size(); i++) {
            if (tempMaterialLines.get(i).getQuantity() != null && tempMaterialLines.get(i).getQuantity().longValue() > 0) {
                materialLine = materialRequestBeanLocal.createMaterialLine(tempMaterialLines.get(i).getRawMaterial().getMaterialType(), tempMaterialLines.get(i).getQuantity());
                materialLineList.add(materialLine);
            }
        }

        Long id = materialRequestBeanLocal.createMaterialRequest(materialLineList);

        if (id == null) {
            statusMessage = "Please Try Again.";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccessful:" + statusMessage + "", ""));
        } else {
            boolean isCompleted = weeklyMaterialReqBeanLocal.changeState(selectedWeeklyMaterialReq.getId());
            if (isCompleted) {
                statusMessage = "New Material Request " + id + " Saved Successfully.";
            } else {
                statusMessage = "Error Ocurrs!";
            }
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
        }
    }
}
